package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuInfo> implements SpuService {

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuPosterMapper spuPosterMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Override
    public IPage<SpuInfo> getPage(Page<SpuInfo> spuInfoPage, Long categoryId) {

        QueryWrapper<SpuInfo> spuInfoQueryWrapper = new QueryWrapper<>();
        spuInfoQueryWrapper.eq("category3_id", categoryId);
        spuInfoQueryWrapper.orderByDesc("id");

        IPage<SpuInfo> spuInfoPage1 = baseMapper.selectPage(spuInfoPage, spuInfoQueryWrapper);

        return spuInfoPage1;
    }

    /**
     * 保存spu
     *
     * @param spuInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {

        // 首先添加spuInfo
        baseMapper.insert(spuInfo); // mybatis是自动主键回填的

        // 添加spuImage
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.stream().forEach(spuImage -> {
                // 设置spuid
                spuImage.setSpuId(spuInfo.getId());
                // 添加spuImage
                spuImageMapper.insert(spuImage);
            });
        }

        //添加spuPoster
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.stream().forEach(spuPoster -> {
                // 设置spuid
                spuPoster.setSpuId(spuInfo.getId());

                //添加spuPoster
                spuPosterMapper.insert(spuPoster);
            });
        }

        // 添加销售属性，以及销售属性值
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            //首先添加销售属性
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                // 设置spuid
                spuSaleAttr.setSpuId(spuInfo.getId());

                spuSaleAttrMapper.insert(spuSaleAttr);

                // 在添加销售属性值
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        // 设置spuid
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        //销售属性id
                        spuSaleAttrValue.setBaseSaleAttrId(spuSaleAttr.getBaseSaleAttrId());
                        //添加销售属性值
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    });

                }

            });
        }

    }

    /**
     * 修改数据前回显
     *
     * @param id
     * @return
     */
    @Override
    public SpuInfo getSpuInfo(Long id) {

        // 先根据id查询spuinfo
        SpuInfo spuInfo = baseMapper.selectById(id);
        if (spuInfo != null) {
            // 根据spuid 查询图片信息
            QueryWrapper<SpuImage> spuImageQueryWrapper = new QueryWrapper<>();
            spuImageQueryWrapper.eq("spu_id", id);
            List<SpuImage> spuImages = spuImageMapper.selectList(spuImageQueryWrapper);

            spuInfo.setSpuImageList(spuImages);

            // 查询商品海报
            QueryWrapper<SpuPoster> spuPosterQueryWrapper = new QueryWrapper<>();
            spuPosterQueryWrapper.eq("spu_id", id);
            List<SpuPoster> spuPosters = spuPosterMapper.selectList(spuPosterQueryWrapper);
            spuInfo.setSpuPosterList(spuPosters);

            // 查询销售属性和销售属性值
            QueryWrapper<SpuSaleAttr> spuSaleAttrQueryWrapper = new QueryWrapper<>();
            spuSaleAttrQueryWrapper.eq("spu_id", id);
            List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrMapper.selectList(spuSaleAttrQueryWrapper);

            spuSaleAttrs.stream().forEach(spuSaleAttr -> {
                // 查询销售属性值
                QueryWrapper<SpuSaleAttrValue> spuSaleAttrValueQueryWrapper = new QueryWrapper<>();
                spuSaleAttrValueQueryWrapper.eq("spu_id", id);
                spuSaleAttrValueQueryWrapper.eq("base_sale_attr_id", spuSaleAttr.getBaseSaleAttrId());
                List<SpuSaleAttrValue> spuSaleAttrValues = spuSaleAttrValueMapper.selectList(spuSaleAttrValueQueryWrapper);
                spuSaleAttr.setSpuSaleAttrValueList(spuSaleAttrValues);
            });

            spuInfo.setSpuSaleAttrList(spuSaleAttrs);
            return spuInfo;
        }

        return null;
    }


    /**
     * 修改商品数据
     *
     * @param spuInfo
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpuInfo(SpuInfo spuInfo) {
        if (spuInfo != null) {
            baseMapper.updateById(spuInfo);

            // 将image poster 和销售属性先删除在添加
            QueryWrapper<SpuImage> spuImageQueryWrapper = new QueryWrapper<>();
            spuImageQueryWrapper.eq("spu_id", spuInfo.getId());
            spuImageMapper.delete(spuImageQueryWrapper);

            // 删除poster
            QueryWrapper<SpuPoster> spuPosterQueryWrapper = new QueryWrapper<>();
            spuPosterQueryWrapper.eq("spu_id", spuInfo.getId());
            spuPosterMapper.delete(spuPosterQueryWrapper);

            //删除销售属性和销售属性值
            QueryWrapper<SpuSaleAttrValue> spuSaleAttrValueQueryWrapper = new QueryWrapper<>();
            spuSaleAttrValueQueryWrapper.eq("spu_id", spuInfo.getId());
            spuSaleAttrValueMapper.delete(spuSaleAttrValueQueryWrapper);

            QueryWrapper<SpuSaleAttr> spuSaleAttrQueryWrapper = new QueryWrapper<>();
            spuSaleAttrQueryWrapper.eq("spu_id", spuInfo.getId());
            spuSaleAttrMapper.delete(spuSaleAttrQueryWrapper);

            // 添加数据
            // 添加spuImage
            List<SpuImage> spuImageList = spuInfo.getSpuImageList();
            if (!CollectionUtils.isEmpty(spuImageList)) {
                spuImageList.stream().forEach(spuImage -> {
                    // 设置spuid
                    spuImage.setSpuId(spuInfo.getId());
                    // 添加spuImage
                    spuImageMapper.insert(spuImage);
                });
            }

            //添加spuPoster
            List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
            if (!CollectionUtils.isEmpty(spuPosterList)) {
                spuPosterList.stream().forEach(spuPoster -> {
                    // 设置spuid
                    spuPoster.setSpuId(spuInfo.getId());

                    //添加spuPoster
                    spuPosterMapper.insert(spuPoster);
                });
            }

            // 添加销售属性，以及销售属性值
            List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
            if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
                //首先添加销售属性
                spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                    // 设置spuid
                    spuSaleAttr.setSpuId(spuInfo.getId());

                    spuSaleAttrMapper.insert(spuSaleAttr);

                    // 在添加销售属性值
                    List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                    if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                        spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                            // 设置spuid
                            spuSaleAttrValue.setSpuId(spuInfo.getId());
                            //销售属性id
                            spuSaleAttrValue.setBaseSaleAttrId(spuSaleAttr.getBaseSaleAttrId());
                            //添加销售属性值
                            spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                        });

                    }

                });

            }
        }
    }
}
